<?php


class EditApp{
	
	public $id=0;
	public $application_id=0;
	public $nome="";
	public $detalhe="";
	public $url="";
	public $user_id=0;
	public $linguagem="";
	public $codigo_fonte_livre=0;
	public $order=0;
	public $rowslist;
	public $application_classification_id=0;
	
	public function __construct(){

		
		switch ($_SERVER['REQUEST_METHOD']) {
			case 'GET':
				
				switch( $_GET['action']){
					case 'edit':
						
						$application_id	=	$_GET['application_id'];
						$result			=	$this->getApplication( $application_id );
						
						$this->id		=	$application_id;
						$this->nome		=	$result->nome;						
						$this->detalhe	=	$result->detalhe;	
						$this->url		=	$result->url;	
						$this->user_id	=	$result->user_id;	
						$this->linguagem=	$result->linguagem;	
						$this->codigo_fonte_livre=	$result->codigo_fonte_livre;
						$this->application_classification_id= $result->application_classification_id;
						
					case 'del':
						
						
						break;

						
					default:
						
						global $app;

						$this->rowslist		=	$this->getRows( $app->getUser_id() );
						
						
						break;
				}
				
				
				break;
			case 'POST':
										
				
				switch( $_POST['action']){
					case 'edit':
						
						global $app;
						
						$_POST['user_id']=	$app->getUser_id();
						$result			=	$this->setApplication( (object) $_POST );						
					
						if(empty($_POST['application_id'])){
							
						}else{
							$this->id		=	$_POST['application_id'];
						}
						
						
						$this->nome		=	$_POST['nome'];						
						$this->detalhe	=	$_POST['detalhe'];	
						$this->url		=	$_POST['url'];	
						$this->user_id	=	$_POST['user_id'];	
						$this->linguagem=	$_POST['linguagem'];	
						$this->codigo_fonte_livre=	$_POST['codigo_fonte_livre'];
						$this->application_classification_id= $_POST['application_classification_id'];
												
						
						header("Location: gridApp.php", TRUE, 301); 
						
						break;
						
					case 'del':
						
						global $app;
						
						$user_id		=	$app->getUser_id();
						$result			=	$this->removeApplication( $_POST['application_id'], $user_id );
						
						header("Location: index.php", TRUE, 301); 
						
						break;
						
					default:
						
						break;
				}
				
				
				break;
			default:
				exit("Caia fora");
				break;
		}
		

		
	}

	
	function splitWhere( $where )
	{
		
		return ( count( $where ) ? 
								' WHERE ' . implode( ' AND ', $where ) : 
								'' );
	}
		
	
	public function getGrid( $rows ){
		
		$result = "";
		
		$result .="<tr>";
							
		$result .=	"<td>Ordem</td>";
		$result .=	"<td>Nome</td>";
		$result .=	"<td>Detalhe</td>";
		$result .=	"<td>URL</td>";
		$result .=	"<td>Linguagem</td>";
		$result .=	"<td>classificacao</td>";
		$result .=	"<td>Código Fonte Disponível</td>";
		
		$result .="</tr>";
			
		
		foreach( $rows as $key=>$item){
			$i++;
			$result .="<tr>";
							
			$result .=	"<td>$i</td>";
			$result .=	"<td><a href='editApp.php?action=edit&application_id=".$item->id."'><img src='images/icon/edit.png' border='0'></a>".$item->nome."</td>";
			$result .=	"<td>".$item->detalhe."</td>";
			$result .=	"<td><a target='_blank' href='".$item->url."'>".$item->url."</a></td>";
			$result .=	"<td>".$item->linguagem."</td>";
			$result .=	"<td>".$item->application_classification."</td>";
			$result .=	"<td>".($item->codigo_fonte_livre=="1"? "sim":"não")."</td>";
			
			$result .="</tr>";
		}
		
		
		
		return "<table border=1 border=1 width='100%'>".$result."</table>";
	}	
	
	
			
	function getRows( $user_id=0, $limit_start=0, $limit_end=10 )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );

		$where	=	array();

		$where[]=	'a.user_id = \''.$user_id.'\'';
		
		$where	=	$this->splitWhere( $where );
				
																
		$query 	=	'SELECT '
					. ' '	
					. ' a.id, '
					. ' a.nome, '
					. ' a.detalhe, '	
					. ' a.user_id, '
					. ' a.url, '		
					. ' a.linguagem, '
					. ' b.nome application_classification '
					. ' '
					. ' FROM '.$config->db_table_semantic_web_application.' a '
					. ' LEFT JOIN '.$config->db_table_semantic_web_application_classification.' b ON '
					. ' a.application_classification_id =b.id '
					. $where;

		$rows	=	$db->getQuery($query);
		
		if(count( $rows )>0 )
			$result=	$rows;

		
		return $result;
	}
	
			
	function getApplication( $application_id=0 )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );

		$where	=	array();

		$where[]=	'id = \''.$application_id.'\'';
		
		$where	=	$this->splitWhere( $where );
				
																
		$query 	=	'SELECT '
					. ' '				
					. ' * '
					. ' '
					. ' FROM '.$config->db_table_semantic_web_application.' '
					. $where;

		$rows	=	$db->getQuery($query);
		
		if(count( $rows )>0 )
			$result=	$rows[0];
		
		return $result;
	}
	
	
	
	function removeApplication( $application_id, $user_id )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );
												
		
		$where	=	array();
			
		$where[]=	'id = '.$application_id.'';
		$where[]=	'user_id = '.$user_id.'';
		
		$where	=	$this->splitWhere( $where );
		
		
		$result			=	$this->getApplication( $args->application_id );
		
		if( empty($result) ){
			//insert
			
			$query 	=	'DELETE FROM '
						. '  '.$config->db_table_semantic_web_application.' '
						.$where;
						
		}


		$rows	=	$db->setQuery($query);
		
		if(count( $rows )>0 )
			$result=	$rows[0];
		
		return $result;
	}


	function setApplication( $args )
	{

		$result	=	"";
		$config	=	new Configuration();	
		
		$db 	=	new MySQL( $config->db_server, 
							$config->db_username, 
							$config->db_password, 
							$config->db_name );
												
		
		$where	=	array();
			
		$where[]=	'id = '.$args->application_id.'';
		
		$where	=	$this->splitWhere( $where );
		
		
		$result			=	$this->getApplication( $args->application_id );
		
		if( empty($result ) ){
			//insert
			
			$query 	=	'INSERT INTO '
						. '  '.$config->db_table_semantic_web_application.' '
						.' (nome,detalhe,url,linguagem,codigo_fonte_livre,application_classification_id,user_id) '
						.' values '
						.' ("'.addslashes($args->nome).'",'
						.' "'.addslashes($args->detalhe).'",'
						.' "'.addslashes($args->url).'",'
						.' "'.addslashes($args->linguagem).'",'
						.' '.addslashes($args->codigo_fonte_livre).','
						.' '.addslashes($args->application_classification_id).','
						.' '.addslashes($args->user_id).')';
						
						
		}else{
			//update
																	
			$query 	=	'UPDATE '
						. '  '.$config->db_table_semantic_web_application.' '
						.' SET '
						.' nome="'.addslashes($args->nome).'",'
						.' detalhe="'.addslashes($args->detalhe).'",'
						.' url="'.addslashes($args->url).'",'
						.' linguagem="'.addslashes($args->linguagem).'",'
						.' codigo_fonte_livre='.addslashes($args->codigo_fonte_livre).','
						.' application_classification_id='.addslashes($args->application_classification_id).','
						.' user_id='.addslashes($args->user_id).' '
						. $where;
			
		}
		
		

		$rows	=	$db->setQuery($query);
		
		
		if( empty($result ) )
			$this->id	=	$db->getLastInsertId();			
									
		
		
		if(count( $rows )>0 )
			$result=	$rows[0];
		
		return $result;
	}
	
	
}

?>